"""7.4 IO操作—读写 CSV 文件"""
import csv
'''一、写入CSV文件：test4.csv'''
data=[
    ["姓名","年龄","性别","地址"],
    ["张三","18","男","北京"],
    ["李四","29","女","上海"],
    ["王五","30","男","广州"]
]
# #打开文件
with open("test4.csv","w",newline="",encoding="utf-8") as file:
    #创建CSV写入对象
    writer=csv.writer(file)

    #写入数据(单行写的模式）
    # for row in data:
    #     writer.writerow(row)
    #多行写入的模式
    writer.writerows(data)
    writer.writerow(["张三丰", "100", "男", "北京"])



'''二、读取CSV文件：test4.csv'''
#打开文件
with open("test4.csv","r",newline="",encoding="utf-8") as file:
    #创建CSV读取对象
    reader=csv.reader(file)
    #读取数据
    for row in reader:
        print(row)